/*
 * User: rant
 * Date: 31/12/2006
 *
 *
 */
package laMalinche.actObservable;

import laMalinche.rete.nodes.tailing.TailingManager;
import laMalinche.rete.rule.ActObservable;
import laMalinche.rete.rule.ActListener;
import laMalinche.DevMode;

import java.util.*;

import com.google.inject.Inject;

/**
 *
 */
public abstract class BasicActObservable implements ActObservable {
    private final Set<ActListener> actListeners = new HashSet<ActListener>();

    @Inject
    private TailingManager tailingManager;

    protected abstract Set<String> getExcludedFields();

    protected BasicActObservable() {
        DevMode.getInstance().getInjector().injectMembers(this);
    }

    protected void adviseStarted() {
        for (ActListener listener : actListeners) {
            listener.actStarted();
        }
    }

    protected void adviseFinished(boolean executed) {
        for (ActListener listener : actListeners) {
            listener.actFinished();
        }
    }

    public void addActListener(ActListener actListener) {
        actListeners.add(actListener);
        if (actListener instanceof TailingManager) {
            final Set<String> list = getExcludedFields();
            if (list != null && !list.isEmpty()) {
                ((TailingManager) actListener).setExcludedProperties(list);
            }
        }
    }

    public TailingManager getTailingManager() {
        tailingManager.setActObservable(this);
        addActListener(tailingManager);
        return tailingManager;
    }
}